<?php

/*
	info: 系统公用调用处理文件
*/

@set_magic_quotes_runtime(0);
@ini_set('memory_limit', '64M');
$db = '';	//数据库类对象预定义
$user = array();	//用户信息数组
$webcache = array();
$webglobal = array();
$webserver = array();
$webcookie = array();
$webupfile = array();
$webrequest = array();
$weblang = array();
$webserver['beginmtime'] = explode(' ', microtime());
$webserver['timestamp'] = time();

define('WEBINROOT', TRUE);
define('WEBFILEPATH', substr(dirname(__FILE__), 0, -8));
define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());

if (PHP_VERSION < '4.1.0')
{
	$_GET = &$HTTP_GET_VARS;
	$_POST = &$HTTP_POST_VARS;
	$_COOKIE = &$HTTP_COOKIE_VARS;
	$_SERVER = &$HTTP_SERVER_VARS;
	$_ENV = &$HTTP_ENV_VARS;
	$_FILES = &$HTTP_POST_FILES;
}

require WEBFILEPATH.'/config.inc.php';
require WEBFILEPATH.'/include/global.func.php';
require WEBFILEPATH.'/include/cache.func.php';
require WEBFILEPATH.'/include/template.func.php';
require WEBFILEPATH.'/include/mysql.class.php';
require WEBFILEPATH.'/include/private.func.php';

//获取参数处理
foreach (array('_POST', '_GET') as $temprequest)
{
	foreach ($$temprequest as $tempkey => $tempvalue)
	{
		$webrequest[$tempkey] = global_Addslashes($tempvalue);
	}
}
$tempprelength = strlen($webconfig['cookiepre']);
foreach ($_COOKIE as $tempkey => $tempvalue)
{
	if (substr($tempkey, 0, $tempprelength) == $webconfig['cookiepre'])
	{
		$webcookie[(substr($tempkey, $tempprelength))] = global_Addslashes($tempvalue);
	}
}
if (!empty($_FILES))
{
	$webupfile = global_Addslashes($_FILES);
}
if (!defined('NOT_UNSETREQUEST'))
{
	unset($HTTP_GET_VARS, $_GET, $HTTP_POST_VARS, $_POST, $HTTP_COOKIE_VARS, $_COOKIE, $HTTP_POST_FILES, $_FILES, $_REQUEST);
}

$webglobal['action'] = isset($webrequest['action']) ? trim($webrequest['action']) : '';
$webglobal['doit'] = isset($webrequest['doit']) ? intval($webrequest['doit']) : 0;
$webglobal['gourl'] = isset($webrequest['gourl']) ? trim($webrequest['gourl']) : '';
$webrequest['page'] = isset($webrequest['page']) ? max(1, intval($webrequest['page'])) : 1;
$webrequest['isforce'] = isset($webrequest['isforce']) ? TRUE : FALSE;
$webrequest['pagehead'] = isset($webrequest['pagehead']) ? trim($webrequest['pagehead']) : '';

//强制头部
if (defined('NOT_HTMLPAGE') || !empty($webrequest['pagehead']))
{
	$temppagehead = !empty($webrequest['pagehead']) ? $webrequest['pagehead'] : (is_string(NOT_HTMLPAGE) ? NOT_HTMLPAGE : '');
	if (!defined('NOT_HTMLPAGE'))
	{
		define('NOT_HTMLPAGE', $temppagehead);
	}
	switch ($temppagehead)
	{
		case 'ajax':
		case 'json':
			@header("Expires: -1");
			@header("Cache-Control: no-store, private, post-check=0, pre-check=0, max-age=0", FALSE);
			@header("Pragma: no-cache");
			header("Content-Type: application/json; charset=".$webconfig['webcharset']);
			break;
		case 'html':
			header("Content-Type: text/html; charset=".$webconfig['webcharset']);
			break;
		default:
			break;
	}
}
else
{
	header("Content-Type: text/html; charset=".$webconfig['webcharset']);
}

//系统定义参数
$webglobal['htmltitle'] = $webglobal['htmldescription'] = '';
$webglobal['htmlkeyword'] = $webglobal['htmlhead'] = $webglobal['htmlnavigation'] = array();
$webglobal['page'] = array();
$webglobal['encodetime'] = urlencode(global_EncryptKey($webserver['timestamp'], WEBAUTHKEY));

$webserver['showtime'] = global_FormatTime($webserver['timestamp']);
$webserver['webhost'] = $_SERVER['HTTP_HOST'];
$webserver['runfile'] = isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
$webserver['curfilename'] = strtolower(basename($webserver['runfile'], '.php'));
$webserver['getstr'] = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : ((isset($_SERVER['argv']) && !empty($_SERVER['argv'])) ? $_SERVER['argv'][0] : '');
$webserver['geturl'] = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $webserver['runfile'].(!empty($webserver['getstr']) ? '?'.$webserver['getstr'] : '');
$webserver['comeurl'] = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
$webserver['pageurl'] = 'http://'.$webserver['webhost'].$webserver['geturl'];
$webglobal['pagesign'] = $webserver['curfilename'].'_'.$webglobal['action'];

// 用户IP获取
if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown'))
{
	$user['ip'] = getenv('HTTP_CLIENT_IP');
}
elseif (getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown'))
{
	$user['ip'] = getenv('HTTP_X_FORWARDED_FOR');
}
elseif (getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown'))
{
	$user['ip'] = getenv('REMOTE_ADDR');
}
elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown'))
{
	$user['ip'] = $_SERVER['REMOTE_ADDR'];
}
preg_match("/[\d\.]{7,15}/", $user['ip'], $tempipmatches);
$user['ip'] = $tempipmatches[0] ? $tempipmatches[0] : 'unknown';
unset($tempipmatches);

//连接数据库
if (!defined('NOT_MYSQL'))
{
	global_ConnectDB();
}

//检查管理登录
if (!defined('NOT_LOGIN'))
{
	include_once WEBFILEPATH.'/include/user.class.php';
	$objuser = new User();
	$user['islogin'] = $objuser->CheckUser();
	if (!$user['islogin'] && $webserver['curfilename']!='login')
	{
		if (defined('NOT_HTMLPAGE'))
		{
			exit();
		}
		global_GoToUrl(WEBURLPATH."/login.php");
	}
}

//检查用户登录
if (defined('IS_MEMBER') && IS_MEMBER)
{
	include_once WEBFILEPATH.'/include/member.class.php';
	$objmem = new Member();
	$user['m_islogin'] = $objmem->CheckUser();
}
